package com.zhentao.day03.pm;

/**
 * 文具店类
 * 实现StockAndSell接口，重写sell( )和stock( )
 * 提供数组，存放多个文具对象
 * 提供展示方法，显示所有文具信息
 * 提供判断某个文具是否存在的方法
 *    参数是文具名称，找到返回下标，未找到返回-1
 */
public class Shop implements StockAndSell {
  /**
   * 存放多个文具对象
   */
  private Stationery [] stationeries ;

  public Shop (){
    stationeries = new Stationery[100] ;
  }

  public Stationery [] getStationeries (){
    return  this.stationeries ;
  }

  /**
   * 根据文具的名称返回数组中的下标索引
   * @param name 文具名称
   * @return 文具名称所对应的下标值, 未找到则返回 -1
   */
  public int find ( String name ){
    int num = -1 ;
    if (stationeries != null){
      for (int i = 0; i < stationeries.length; i++) {
        // 数组中的每一个文具如果存在的话, 那么就判断文具的名称是否相同
        if(stationeries[i] != null ){
          Stationery stationery = stationeries[i];
          if(stationery.getName().equals(name)){
            num = i ;
            break;
          }
        }
      }
    }
    return num ;
  }

  /**
   * 展示所有文具信息
   */
  public void show(){
    if (stationeries != null) {
      for (int i = 0; i < stationeries.length; i++) {
        Stationery stationery = stationeries[i];
        if (stationery != null && stationery instanceof Pen){
          Pen pen = (Pen) stationery;
          pen.show();
        }

        if (stationery != null && stationery instanceof NotePad){
          NotePad notePad = (NotePad) stationery;
          notePad.show();
        }
      }
    }
  }

  @Override
  public void sell(Stationery stationeries [],int i,int num) {
      if(stationeries[i].getNum() >= num ){
        stationeries[i].setNum(stationeries[i].getNum() - num);
        System.out.println("文具已售出");
      }else{
        System.out.println("数量不足, 无法售出");
      }
  }

  @Override
  public void stock(Stationery stationery) {
    System.out.println(stationery);
    System.out.println(stationeries.length);
    if(stationeries !=null){
      // 在数组中添加内容
      for (int i = 0; i < stationeries.length; i++) {
        if(stationeries[i] == null){
          stationeries[i] = stationery ;
          break;
        }
      }
    }

    
    
  }
}
